package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.activity.sife_secondactivity.OneSifeSecondActivity;
import com.wangtao.firstapp.modles.ShopDetailsData;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 单品详情页面-->相关文章 的适配器
 * Created by lanouhn on 16/9/10.
 */
public class ShopEssayAdapter extends RecyclerView.Adapter<ShopEssayAdapter.ShopEssayViewHolder> {

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    private Context context;
    private ShopDetailsData shopDetailsData;


    public ShopEssayAdapter(Context context, ShopDetailsData shopDetailsData) {
        this.context = context;
        this.shopDetailsData = shopDetailsData;
    }

    @Override
    public ShopEssayViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shopdetails_essay, null);
        ShopEssayViewHolder holder = new ShopEssayViewHolder(view);
        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();
        return holder;
    }

    @Override
    public void onBindViewHolder(ShopEssayViewHolder holder, final int position) {
        final List<ShopDetailsData.DataBean.ArticleTopicListBean> articleTopicListBean = shopDetailsData.getData().getArticle_topic_list();

        if (!shopDetailsData.getData().getArticle_topic_count().equals("")){

            holder.relTitle.setVisibility(View.VISIBLE);
            Picasso.with(context).load(articleTopicListBean.get(position).getPics().get(0).getUrl()).into(holder.imageBig);
            holder.title.setText(articleTopicListBean.get(position).getTitle());
            holder.brose.setText("浏览 " + articleTopicListBean.get(position).getViews());
            holder.comment.setText("评论 " + articleTopicListBean.get(position).getComments());
            Picasso.with(context).load(articleTopicListBean.get(position).getUser().getAvatar()).into(holder.logo);
            holder.name.setText(articleTopicListBean.get(position).getUser().getNickname());
            holder.time.setText(articleTopicListBean.get(position).getCreate_time_str());
        }

        //跳转至，，文章页面,,作者原创
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String item_ID = articleTopicListBean.get(position).getId();
                Intent intent = new Intent(context, OneSifeSecondActivity.class);
                intent.putExtra("item_ID", item_ID);
                context.startActivity(intent);
            }
        });

        //跳转至 作者介绍页面
        holder.logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String author_ID = articleTopicListBean.get(position).getUser().getUser_id();
                editor.putString("author_iD", author_ID);
                editor.commit();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                context.startActivity(intent);
            }
        });

        //跳转至 作者介绍页面
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String author_ID = articleTopicListBean.get(position).getUser().getUser_id();
                editor.putString("author_iD", author_ID);
                editor.commit();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);

                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return shopDetailsData.getData().getArticle_topic_list().size();
    }

    public class ShopEssayViewHolder extends RecyclerView.ViewHolder {
        private RelativeLayout relTitle, relEssay;
        private ImageView imageBig;
        private TextView essay, title, brose, comment, name, time;
        private CircleImageView logo;

        public ShopEssayViewHolder(View itemView) {
            super(itemView);
            //relEssay = (RelativeLayout) itemView.findViewById(R.id.relEssay_item_shopdetails_essay);//最上部的标题，点击后跳转至全部文章页面
            //essay = (TextView) itemView.findViewById(R.id.essay_item_shopdetails_essay);//相关文章(31)，，
            relTitle = (RelativeLayout) itemView.findViewById(R.id.relTitle_item_shopdetails_essay);//下边的数据部分的容器，用于隐藏，显示
            imageBig = (ImageView) itemView.findViewById(R.id.imageBig_item_shopdetails_essay);//大图
            title = (TextView) itemView.findViewById(R.id.title_item_shopdetails_essay);//标题
            brose = (TextView) itemView.findViewById(R.id.brose_item_shopdetails_essay);//浏览人数
            comment = (TextView) itemView.findViewById(R.id.comment_item_shopdetails_essay);//评论数
            logo = (CircleImageView) itemView.findViewById(R.id.logo_item_shopdetails_essay);//作者头像
            name = (TextView) itemView.findViewById(R.id.name_item_shopdetails_essay);//作者昵称
            time = (TextView) itemView.findViewById(R.id.time_item_shopdetails_essay);//时间
        }
    }
}
